-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add getPort to resolve Port(0) #17559
Conversation
518c1e0
to
25ebe47
Compare
25ebe47
to
4315108
Compare
lib/pure/asynchttpserver.nim
Outdated
func getSocket*(self: AsyncHttpServer): AsyncSocket {.since: (1, 5, 1).} = | ||
## Field accessor. | ||
runnableExamples: | ||
from std/asyncdispatch import Port | ||
from std/asyncnet import getFd | ||
from std/nativesockets import getLocalAddr, AF_INET | ||
let server = newAsyncHttpServer() | ||
server.listen(Port(0)) # Socket is not bound until this point | ||
let port = getLocalAddr(server.getSocket.getFd, AF_INET)[1] | ||
doAssert uint16(port) > 0 | ||
# note: a more direct way to get the port is `getPort`. | ||
let (laddr, port) = getLocalAddr(server.getSocket.getFd, AF_INET) | ||
assert uint16(port) > 0 | ||
assert laddr == "0.0.0.0" | ||
server.close() | ||
a.socket | ||
self.socket |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like there isn't much reason to keep this now that we have getPort
, I don't see another reason why it would be useful. Since it was introduced in the same version it shouldn't cause breakage, can we remove it in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe but it belongs in a separate PR, I've added a future work item to address this. getPort doesn't subsume getSocket, so that should be discussed separately. eg, it shows you can use this to get assert laddr == "0.0.0.0"
in this example and there may be other use cases for getSocket.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Argh, I don't see a reason not to do this in the same PR. But here you go: #17587
2bde084
to
c2c97c8
Compare
PTAL |
I also updated the module-level example to promote best practices and avoid issues like
Address already in use
caused by hard coded ports as explained in #14327future work
getSocket
now that we havegetPort
=> Removes asynchttpserver.getSocket. #17587